home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MPW_TOOL / TOOLS / TOOLS_WI / ICON_8 / ICONT_FO / UTIL.C < prev   
Text File  |  1990-03-02  |  6KB  |  313 lines

  1. /*
  2.  *  util.c -- general utility functions.
  3.  */
  4.  
  5. #include <ctype.h>
  6. #include "::h:config.h"
  7. #include "tproto.h"
  8. #include "::h:cpuconf.h"
  9. #include "globals.h"
  10. #include "general.h"
  11. #include "trans.h"
  12. #include "tree.h"
  13.  
  14.  
  15. extern int optind;
  16.  
  17. extern char *ofile;
  18.  
  19. /*
  20.  * The following code is operating-system dependent [@util.01].  Define the
  21.  *  characters that terminate a file name prefix.
  22.  */
  23.  
  24. #if PORT
  25. #define Prefix "/"
  26. Deliberate Syntax Error
  27. #endif                    /* PORT */
  28.  
  29. #if AMIGA
  30. #define Prefix "/:"
  31. #endif                    /* AMIGA */
  32.  
  33. #if ATARI_ST
  34. #define Prefix "/:\\"
  35. #endif                    /* ATARI_ST */
  36.  
  37. #if HIGHC_386 || MSDOS || OS2
  38. #define Prefix "/:\\"
  39. #endif                    /* HIGHC_386 || MSDOS || OS2 */
  40.  
  41. #if MACINTOSH
  42. #define Prefix ":"
  43. #endif                    /* MACINTOSH */
  44.  
  45. #if MVS || VM
  46. #define Prefix ""
  47. #endif                    /* MVS || VM */
  48.  
  49. #if UNIX
  50. #define Prefix "/"
  51. #endif                    /* UNIX */
  52.  
  53. #if VMS
  54. #define Prefix "]:"
  55. #endif                    /* VMS */
  56.  
  57. /*
  58.  * End of operating-system specific code.
  59.  */
  60.  
  61. /*
  62.  * Information about Icon functions.
  63.  */
  64.  
  65. /*
  66.  * Number of arguments.
  67.  */
  68.  
  69.  
  70. /*
  71.  * Names of Icon functions.
  72.  */
  73. char *ftable[] = {
  74. #ifdef PreProcess
  75. /* define(FncDef,"$1"`,') */
  76. /* define(FncDefV,"$1"`,') */
  77. /* include(../h/fdefs.h) /* */
  78. /* undefine(`FncDef') */
  79. /* undefine(`FncDefV') */
  80. /* */
  81. #else                    /* PreProcess */
  82. #define FncDef(p,n) Lit(p),
  83. #define FncDefV(p) Lit(p),
  84. #include "::h:fdefs.h"
  85. #undef FncDef
  86. #undef FncDefV
  87. #endif                    /* PreProcess */
  88.    };
  89.  
  90. int ftbsize = sizeof(ftable)/sizeof(char *);
  91.  
  92. /*
  93.  * alloc - allocate n bytes
  94.  */
  95.  
  96. pointer alloc(n)
  97. unsigned int n;
  98.    {
  99.    pointer a;
  100.  
  101.    if (!(a = malloc((msize)n)))
  102.       quit("out of memory");
  103.    return a;
  104.    }
  105.  
  106. /*
  107.  * salloc - allocate and initialize string 
  108.  */
  109.  
  110. char *salloc(s)
  111. char *s;
  112.    {
  113.  
  114.    return strcpy((char *)alloc((unsigned int)(strlen(s)+1)),s);
  115.    }
  116.  
  117. /*
  118.  * tcalloc - allocate and zero m*n bytes
  119.  */
  120. pointer tcalloc(m,n)
  121. unsigned int m, n;
  122.    {
  123.    pointer a;
  124.  
  125.    if (!(a = calloc(m,n)))
  126.       quit("out of memory");
  127.    return a;
  128.    }
  129.  
  130. /*
  131.  * fparse - break a file name down into component parts.
  132.  *  Result is a pointer to a struct of static pointers good until the next call.
  133.  */
  134. struct fileparts *fparse(s)
  135. char *s;
  136.    {
  137.    static char buf[MaxFileName+2];
  138.    static struct fileparts fp;
  139.    int n;
  140.    char *p, *q;
  141.    char *index();
  142.  
  143.    q = s;
  144.    fp.ext = p = s + strlen(s);
  145.    while (--p >= s) {
  146.       if (*p == '.' && *fp.ext == '\0')
  147.          fp.ext = p;
  148.       else if (index(Prefix,*p)) {
  149.          q = p+1;
  150.          break;
  151.          }
  152.       }
  153.    fp.dir = buf;
  154.    n = q - s;
  155.    strncpy(fp.dir,s,n);
  156.    fp.dir[n] = '\0';
  157.    fp.name = buf + n + 1;
  158.    n = fp.ext - q;
  159.    strncpy(fp.name,q,n);
  160.    fp.name[n] = '\0';
  161.    return &fp;
  162.    }
  163.  
  164. /*
  165.  * makename - make a file name, optionally substituting a new dir and/or ext
  166.  */
  167. char *makename(dest,d,name,e)
  168. char *dest, *d, *name, *e;
  169.    {
  170.    struct fileparts fp;
  171.    fp = *fparse(name);
  172.    if (d != NULL)
  173.       fp.dir = d;
  174.    if (e != NULL)
  175.       fp.ext = e;
  176.    sprintf(dest,"%s%s%s",fp.dir,fp.name,fp.ext);
  177.    return dest;
  178.    }
  179.  
  180. /*
  181.  * quit - immediate exit with error message
  182.  */
  183.  
  184. novalue quit(msg)
  185. char *msg;
  186.    {
  187.    quitf(msg,"");
  188.    }
  189.  
  190. /*
  191.  * quitf - immediate exit with message format and argument
  192.  */
  193. novalue quitf(msg,arg)
  194. char *msg, *arg;
  195.    {
  196.  
  197.  
  198.    extern char *progname;
  199.    fprintf(stderr,"%s: ",progname);
  200.    fprintf(stderr,msg,arg);
  201.    fprintf(stderr,"\n");
  202.  
  203. #ifndef VarTran
  204.    unlink(ofile);            /* remove bad icode file */
  205. #endif                    /* VarTran */
  206.  
  207.    exit(ErrorExit);
  208.    }
  209.  
  210. /*
  211.  * tsyserr is called for fatal errors.  The message s is produced and the
  212.  *  translator exits.
  213.  */
  214. novalue tsyserr(s)
  215. char *s;
  216.    {
  217.  
  218.  
  219.    if (tok_loc.n_file)
  220.       fprintf(stderr, "File %s; ", tok_loc.n_file);
  221.    fprintf(stderr, "Line %d # %s\n", in_line, s);
  222.  
  223.    exit(ErrorExit);
  224.    }
  225.  
  226.  
  227. /*
  228.  * round2 - round an integer up to the next power of 2.
  229.  */
  230. unsigned int round2(n)
  231. unsigned int n;
  232.    {
  233.    unsigned int b = 1;
  234.    while (b < n)
  235.       b <<= 1;
  236.    return b;
  237.    }
  238.  
  239.  
  240. /*
  241.  * sizearg - process -S command option.
  242.  */
  243.  
  244. struct keyptr {            /* structure for listing option chars */
  245.    char *cmd;                /* option character(s) */
  246.    unsigned int *valp;            /* pointer to value word */
  247.    };
  248.  
  249. static struct keyptr keytable[] = {    /* maps keys to store addresses */
  250.  
  251. #define Size(cmd,vname,defalt) cmd, &vname,
  252. #define MinSize(x,y,z)
  253. #include "sizes.h"            /* initialize from "sizes.h" data */
  254. #undef Size
  255. #undef MinSize
  256.    0, 0,                /* terminate with null entry */
  257.    };
  258.  
  259. novalue sizearg(arg,argv)
  260. char *arg;
  261. char **argv;
  262.    {
  263.    struct keyptr *k;            /* key table pointer */
  264.    char *s;                /* value string pointer */
  265.    int v;                /* option value */
  266.  
  267.    for (k = keytable; k->cmd; k++)    /* search for key match */
  268.       if (arg[0] == k->cmd[0] && (arg[0] != 'h' || arg[1] == k->cmd[1]))
  269.          break;
  270.  
  271.    if (k->cmd == NULL)            /* abort if not found */
  272.       quitf("unrecognized -S option: -S%s",arg);
  273.  
  274.    if (arg[0] == 'h')
  275.       s = &arg[2];            /* find value */
  276.    else
  277.       s = &arg[1];
  278.       
  279.    if (*s == '\0') {            /* if value is in next arg */
  280.       s = argv[optind++];
  281.       if (s == NULL)
  282.          quitf("missing value: -S%s", arg);
  283.       }
  284.  
  285.    v = (int)atol(s);                /* convert integer -- check */
  286.    if (v <= 0)
  287.       quitf("illegal value: -S%s", arg);
  288.    *k->valp = v;            /* store result */
  289.    }
  290.  
  291.  
  292. /*
  293.  * smatch - case-insensitive string match - returns nonzero if they match
  294.  */
  295. int smatch(s,t)
  296. char *s, *t;
  297.    {
  298.    char a, b;
  299.    for (;;) {
  300.       while (*s == *t)
  301.          if (*s++ == '\0')
  302.             return 1;
  303.          else
  304.             t++;
  305.       a = *s++;
  306.       b = *t++;
  307.       if (isupper(a))  a = tolower(a);
  308.       if (isupper(b))  b = tolower(b);
  309.       if (a != b)
  310.          return 0;
  311.       }
  312.    }
  313.